home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mntlib44.zoo / mntlib / truncate.c < prev    next >
C/C++ Source or Header  |  1993-11-03  |  2KB  |  90 lines

  1. /*
  2.  
  3. Here is an implementation of truncate/ftruncate for the MiNTlib. There
  4. is a special case for truncate, if the filesystem does not recognize
  5. FTRUNCATE and the length is zero, Fcreate is used to truncate the
  6. file. This only works for tosfs if the file isn't already open. The
  7. rest is quite straight forward.
  8.  
  9. Andreas.
  10.  
  11. Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
  12.  
  13. */
  14.  
  15. #include <compiler.h>
  16. #include <limits.h>
  17. #include <errno.h>
  18. #include <mintbind.h>
  19. #include <ioctl.h>
  20. #ifdef __TURBOC__
  21. #include <sys\types.h>
  22. #else
  23. #include <sys/types.h>
  24. #endif
  25. #include "lib.h"
  26.  
  27. extern int __mint;
  28.  
  29. int
  30. truncate (_filename, length)
  31.      const char *_filename;
  32.      off_t length;
  33. {
  34.   int fh, res;
  35.   char filename[PATH_MAX];
  36.  
  37.   (void) _unx2dos (_filename, filename);
  38.   res = -EINVAL;
  39.   if (__mint > 92)
  40.     {
  41.       res = Dcntl(FTRUNCATE, (long) filename, (long) &length);
  42.       if (res != -EINVAL)
  43.         {
  44.           errno = (int) -res;
  45.           return -1;
  46.     }
  47.     }
  48.   fh = (int)Fopen (filename, 2);
  49.   if (fh < 0)
  50.     {
  51.       errno = -fh;
  52.       return -1;
  53.     }
  54.   if (__mint > 90)
  55.     res = (int) Fcntl (fh, (long) &length, FTRUNCATE);
  56.   Fclose (fh);
  57.   if (res == -EINVAL && length == 0)
  58.     {
  59.       res = (int)Fcreate (filename, 0);
  60.       if (res >= 0)
  61.     Fclose (res);
  62.     }
  63.   if (res < 0)
  64.     {
  65.       errno = -res;
  66.       return -1;
  67.     }
  68.   return 0;
  69. }
  70.  
  71. int
  72. ftruncate (fd, length)
  73.      int fd;
  74.      off_t length;
  75. {
  76.   int res;
  77.  
  78.   if (__mint > 90)
  79.     res = (int) Fcntl (fd, (long) &length, FTRUNCATE);
  80.   else
  81.     res = -EINVAL;
  82.  
  83.   if (res < 0)
  84.     {
  85.       errno = -res;
  86.       return -1;
  87.     }
  88.   return 0;
  89. }
  90.